local keji = fk.CreateSkill{
    name = "steam__keji",
    tags={Skill.Compulsory},
  }

Fk:loadTranslationTable{
  ["steam__keji"] = "克己",
  [":steam__keji"] = "锁定技，当你摸牌时，跳过牌堆中的【杀】。若你未于出牌阶段内使用或打出过【杀】，则你跳过弃牌阶段。"
}
keji:addEffect(fk.EventPhaseChanging, {
    anim_type = "defensive",
    can_trigger = function(self, event, target, player, data)
      if target == player and player:hasSkill(keji.name) and data.phase == Player.Discard and not data.skipped then
        local room = player.room
        local logic = room.logic
        local play_ids = logic:getEventsOfScope(GameEvent.Phase, 1, function (e)
          if e.data.phase == Player.Play and e.end_id then
            -- table.insert(play_ids, {e.id, e.end_id})
            return true
          end
          return false
        end, Player.HistoryTurn)
        if #play_ids == 0 then return true end
        ---@param e GameEvent.UseCard | GameEvent.RespondCard
        local function playCheck (e)
          local in_play = false
          for _, phase_event in ipairs(play_ids) do
            if e.id > phase_event.id and e.id < phase_event.end_id then
              in_play = true
              break
            end
          end
          return in_play and e.data.from == player and e.data.card.trueName == "slash"
        end
        return #logic:getEventsOfScope(GameEvent.UseCard, 1, playCheck, Player.HistoryTurn) == 0
        and #logic:getEventsOfScope(GameEvent.RespondCard, 1, playCheck, Player.HistoryTurn) == 0
      end
    end,
    on_use = function(self, event, target, player, data)
      data.skipped = true
    end,
})

keji:addEffect(fk.BeforeDrawCard, {
    anim_type = "drawcard",
    mute=true,
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(keji.name) and data.num>0
    end,
    on_use = function(self, event, target, player, data)
      local room=player.room
      local n=data.num
      local from=data.fromPlace or "top"
      local drawpile=room.draw_pile --生成牌堆中非杀
      -- 预先获取要摸的牌
      local noslash_pile={}--获得非杀牌堆
      for i,cid in ipairs(drawpile) do
        local card=Fk:getCardById(cid)
        if card.trueName ~= "slash" then --若为杀则跳过
           table.insert(noslash_pile,cid)
        end
      end
      if #noslash_pile<n then
        room:shuffleDrawPile()
        noslash_pile={}--获得非杀牌堆
        for i,cid in ipairs(room.draw_pile) do
            local card=Fk:getCardById(cid)
            if card.trueName ~= "slash" then --若为杀则跳过
                table.insert(noslash_pile,cid)
            end
        end
      end
      if #noslash_pile<n then
        return false
      end
      local cardIds = table.slice(noslash_pile, 1, n + 1)
      for i,cid in ipairs(cardIds) do
        table.removeOne(room.draw_pile,cid)
        local j=i
        if from == "bottom" then
          j=#room.draw_pile+1-i
          table.insert(room.draw_pile,j,cid)
        else
          table.insert(room.draw_pile,i,cid)
        end
      end
      
      
     
    end,
})
  
  

  return keji